package com.siashan.unipay4j.ali.splitbill;

import com.alibaba.fastjson.JSON;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.siashan.unipay4j.ali.AbstractAliPay;
import com.siashan.unipay4j.core.IPayConfig;
import com.siashan.unipay4j.core.Result;
import com.siashan.unipay4j.core.splitbill.ISplitBillQuery;
import com.siashan.unipay4j.core.splitbill.ProfitQueryModel;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝分账查询
 *
 * @author siashan
 * @since v1.0.1
 **/
public class AliPaySplitBillQuery extends AbstractAliPay implements ISplitBillQuery {
    public AliPaySplitBillQuery() {
    }

    public AliPaySplitBillQuery(IPayConfig payConfig) {
        super(payConfig);
    }


    /**
     * 分账结果查询
     *
     * @param model  查询参数
     * @return 分账结果
     */
    @Override
    public Result query(ProfitQueryModel model) {
        try {
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            request.setBizContent(JSON.toJSONString(wrapParam(model)));
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println("调用成功");
                response.getTradeStatus();
            } else {
                System.out.println("调用失败");
            }
        } catch (Exception e) {

        }
        return Result.exception();
    }

    /**
     * 封装查询参数
     *
     * @param model 查询请求参数
     * @return 查询参数
     */
    private Map<String, String> wrapParam(ProfitQueryModel model) {
        Map<String, String> map = new HashMap<>();
        map.put("trade_no", model.getTradeNo());
        map.put("out_trade_no", model.getOutTradeNo());
        return map;
    }
}
